import {
  fetchUserTagList
} from '@/services/user/UserCommon';
import {arr2tree} from '@/utils/utils';

export default {
  namespace: 'UserCommon',

  state: {
    userTagList: [],
    userTagTree: [],
  },

  effects: {

    * fetchUserTagList({}, {call, put}) {
      const response = yield call(fetchUserTagList);
      if (response.code === 0) {
        yield put({
          type: 'chgUserTagList',
          payload: response.data,
        });
        yield put({
          type: 'chgUserTagTree',
          payload: arr2tree(response.data.map(d => {
            d.label = d.name;
            d.value = d.id;
            d.key = d.id;
            return d;
          })),
        });
      }
    },


  },

  reducers: {
    chgUserTagList(state, action) {
      return {
        ...state,
        userTagList: action.payload,
      };
    },
    chgUserTagTree(state, action) {
      return {
        ...state,
        userTagTree: action.payload,
      };
    },

  },

};
